﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Rezene.Web.Business;
using Rezene.Web.Business.Amazon;
using Rezene.Web.Models;
using System.Web.Security;
using Rezene.Web.Business.Amazon.Entities;

namespace Rezene.Web.Controllers
{
    public class GalleryController : BaseController
    {
        private ListPageViewModel CreateListPageViewModel(string id)
        {
            var galleries = AmazonRepository.GetGalleries(id);
            var photos = AmazonRepository.GetPhotos(id);
            var model = new ListPageViewModel();
            if (IsAuthorized)
            {
                model.Galleries.AddRange(galleries);
                model.Photos.AddRange(photos);
            }
            else
            {
                model.Galleries.AddRange(galleries.Where(g => g.Show).ToList());
                model.Photos.AddRange(photos.Where(p => p.Show).ToList());
            }
            model.IsAuthorized = IsAuthorized;
            model.CurrentGallery = GetCurrentGalery();
            return model;
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Single(string path)
        {
            var model = new SinglePhotoViewModel();
            var photo = AmazonRepository.GetPhotoInfo(path);
            if (photo != null)
            {
                model.Description = photo.Description;                
                model.Title = photo.DisplayName;
                model.AmazonPath = photo.AmazonPublicPath;
            }
            return View(model);
        }

        public ActionResult BreadCrumb()
        {
            var path = Request.Path;
            var model = new BreadcrumbViewModel();
            var cleanPath = CurrentGalleryPath();
            if (!string.IsNullOrEmpty(cleanPath))
            {                
                var items = cleanPath.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                model.Items.Add(new BreadcrumbItem()
                {
                    Title = "Home",
                    Link = "/"
                });

                foreach (var item in items)
                {
                    var localPath = string.Empty;
                    var amazonPath = cleanPath.Substring(0, cleanPath.IndexOf(item) + item.Length);
                    if (items.Last() != item)
                    {
                        localPath = "/Gallery/List/"+ amazonPath;
                    }
                    var gallery = AmazonRepository.GetGalleryInfo(amazonPath);
                    model.Items.Add(new BreadcrumbItem()
                    {
                        Title = gallery.DisplayName,
                        Link = localPath
                    });
                }
            }

            return PartialView(model);
        }

        public ActionResult List(string id)
        {
            var model = CreateListPageViewModel(id);
            model.IsHome = string.IsNullOrEmpty(id);
            return View("List", model);
        }

        public ActionResult CatchAll()
        {
            var path = Request.Path;
            if (path.Contains("/Gallery/List"))
            {
                var model = CreateListPageViewModel(GetCleanPath(path));
                return View("List", model);
            }

            return HttpNotFound();
        }
    }
}